package cn.achao.csr.chat.dao;

import cn.achao.csr.api.model.entity.ChatGroupMember;
import cn.achao.csr.chat.mapper.ChatGroupMemberMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import lombok.extern.slf4j.Slf4j;
import lombok.RequiredArgsConstructor;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static cn.achao.csr.api.model.enums.GroupRoleEnum.ADMIN_LIST;

@Slf4j
@Service
@RequiredArgsConstructor
public class ChatGroupMemberDao extends ServiceImpl<ChatGroupMemberMapper, ChatGroupMember> {

    public ChatGroupMember getMember(Long groupId, Long uid) {
        return lambdaQuery()
                .eq(ChatGroupMember::getGroupId, groupId)
                .eq(ChatGroupMember::getUid, uid)
                .one();
    }

    public List<Long> getMemberUidList(Long groupId) {
        List<ChatGroupMember> list = lambdaQuery()
                .eq(ChatGroupMember::getGroupId, groupId)
                .select(ChatGroupMember::getUid)
                .list();
        return list.stream().map(ChatGroupMember::getUid).collect(Collectors.toList());
    }

    /**
     * 批量获取成员群角色
     *
     * @param groupId 群ID
     * @param uidList 用户列表
     * @return 成员群角色列表
     */
    public Map<Long, Integer> getMemberMapRole(Long groupId, List<Long> uidList) {
        List<ChatGroupMember> list = lambdaQuery()
                .eq(ChatGroupMember::getGroupId, groupId)
                .in(ChatGroupMember::getUid, uidList)
                .in(ChatGroupMember::getGroupRole, ADMIN_LIST)
                .select(ChatGroupMember::getUid, ChatGroupMember::getGroupRole)
                .list();
        return list.stream().collect(Collectors.toMap(ChatGroupMember::getUid, ChatGroupMember::getGroupRole));
    }
}

